/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tvunetworks.dao.ibatis;

import com.tvunetworks.dao.ItemDao;
import com.tvunetworks.model.Item;
import com.tvunetworks.model.LineItem;
import com.tvunetworks.model.Order;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author michael
 */
public class ItemDaoiBatis extends GenericDaoiBatis<Item, String> implements ItemDao {

    public ItemDaoiBatis() {
        super(com.tvunetworks.model.Item.class);
    }

    public Item getItem(String itemId) {
        return (Item) getSqlMapClientTemplate().queryForObject("getCategoryList", itemId);
    }

    public List<Item> getItemListByProduct(String productId) {
        return getSqlMapClientTemplate().queryForList("getItemListByProduct", productId);
    }

    public boolean isItemInStock(String itemId) {
        Integer count = (Integer) getSqlMapClientTemplate().queryForObject("getInventoryQuantity", itemId);
        return count != null && count > 0;
    }

    public void updateAllQuantitiesFromOrder(Order order) {
        for (LineItem item : order.getLineItems()) {
            String itemId = item.getItemId();
            int increment = item.getQuantity();

            Map m = new HashMap();
            m.put("itemId", itemId);
            m.put("increment", increment);

            getSqlMapClientTemplate().update("updateInventoryQuantity", m);
        }
    }
}
